unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, RzPanel, RzSplit, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters, cxSSheet, Grids,
  BaseGrid, AdvGrid, ImgList, RzButton, RzStatus, RzSpnEdt;

type
  TForm1 = class(TForm)
    RzSplitter1: TRzSplitter;
    AdvGrid: TAdvStringGrid;
    CxExcel: TcxSpreadSheetBook;
    RzToolbar1: TRzToolbar;
    RzToolButton1: TRzToolButton;
    RzToolButton2: TRzToolButton;
    RzToolButton3: TRzToolButton;
    ImageList1: TImageList;
    RzStatusBar1: TRzStatusBar;
    RzStatusPane1: TRzStatusPane;
    RzStatusPane2: TRzStatusPane;
    RzSpinner1: TRzSpinner;
    RzSpinner2: TRzSpinner;
    RzSpacer1: TRzSpacer;
    RzSpacer2: TRzSpacer;
    RzToolButton4: TRzToolButton;
    RzToolButton5: TRzToolButton;
    RzToolButton6: TRzToolButton;
    RzToolButton7: TRzToolButton;
    RzToolButton8: TRzToolButton;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure RzToolButton1Click(Sender: TObject);
    procedure AdvGridSelectionChanged(Sender: TObject; ALeft, ATop, ARight,
      ABottom: Integer);
    procedure RzToolButton2Click(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure RzToolButton3Click(Sender: TObject);
    procedure RzToolButton4Click(Sender: TObject);
    procedure RzToolButton5Click(Sender: TObject);
    procedure RzToolButton6Click(Sender: TObject);
    procedure CxExcelSetSelection(Sender: TObject; ASheet: TcxSSBookSheet);
    procedure RzToolButton7Click(Sender: TObject);
  private
    procedure SetCommParams;
    procedure SetGridParams;
    procedure SetExclParams;
  public
  end;

var
  Form1: TForm1;

implementation

uses
  Class_ExclLoad;

{$R *.dfm}

procedure TForm1.AdvGridSelectionChanged(Sender: TObject; ALeft, ATop, ARight,
  ABottom: Integer);
begin
  with AdvGrid do
  begin
    if IsMergedCell(Selection.Left,Selection.Top) then
    begin
      RzStatusPane1.Caption:=Format('Selection:%D-%D-%D-%D;MergedByRect:%D-%D-%D-%D',[Selection.Left,Selection.Top,Selection.Right,Selection.Bottom,Selection.Left,Selection.Top,CellProperties[Selection.Left,Selection.Top].CellSpanX+1,CellProperties[Selection.Left,Selection.Top].CellSpanY+1]);
    end else
    begin
      RzStatusPane1.Caption:=Format('Selection:%D-%D-%D-%D;CanMergeCell:%D-%D-%D-%D',[Selection.Left,Selection.Top,Selection.Right,Selection.Bottom,Selection.Left,Selection.Top,Selection.Right-Selection.Left+1,Selection.Bottom-Selection.Top+1]);
    end;
  end;
end;

procedure TForm1.CxExcelSetSelection(Sender: TObject; ASheet: TcxSSBookSheet);
begin
  with CxExcel.ActiveSheet.SelectionRect do
  begin
    RzStatusPane1.Caption:=Format('Selection:%D-%D-%D-%D',[Left,Top,Right,Bottom]);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  WindowState:=wsMaximized;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  SetCommParams;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  //SetCommParams;->FormResize
  SetGridParams;
  SetExclParams;
end;

procedure TForm1.RzToolButton1Click(Sender: TObject);
begin
  with AdvGrid do
  begin
    MergeCells(Selection.Left,Selection.Top,Selection.Right-Selection.Left+1,Selection.Bottom-Selection.Top+1);
  end;
end;

procedure TForm1.RzToolButton2Click(Sender: TObject);
begin
  SetGridParams;
end;

procedure TForm1.RzToolButton3Click(Sender: TObject);
var
  ColxA:Integer;
  RowxA:Integer;
begin
  with AdvGrid do
  begin
    ColxA:=RzSpinner1.Value;
    RowxA:=RzSpinner2.Value;

    if CellProperties[ColxA,RowxA].IsBaseCell then
    begin
      ShowMessageFmt('Cells[%D:%D]:=isbasecell',[ColxA,RowxA]);
    end else
    begin
      ShowMessageFmt('Cells[%D,%D]:=nobasecell',[ColxA,RowxA]);
    end;
  end;
end;

procedure TForm1.RzToolButton4Click(Sender: TObject);
var
  I,M:Integer;
  CellA:TcxSSCellObject;
begin
  with AdvGrid,CxExcel do
  begin
    CxExcel.BeginUpdate;
    CxExcel.History.Clear;
    CxExcel.ClearAll;
    for I := 0 to ColCount - 1 do
    begin
      for M := 0 to RowCount - 1 do
      begin
        if IsMergedCell(I,M) then
        begin
          if not CellProperties[I,M].IsBaseCell then Continue;
          CxExcel.ActiveSheet.SetMergedState(Rect(I,M,CellProperties[I,M].CellSpanX+I,CellProperties[I,M].CellSpanY+M),True);
        end;
        CellA:=CxExcel.ActiveSheet.GetCellObject(I,M);
        CellA.Text:=Cells[I,M];
        CellA.Free;
      end;
    end;
    CxExcel.EndUpdate;
  end;
end;

procedure TForm1.RzToolButton5Click(Sender: TObject);
var
  RectA:TRect;
begin
  with CxExcel do
  begin
    RectA.Left:=SelectionRect.Left;
    RectA.Top :=SelectionRect.Top;
    RectA.Right :=SelectionRect.Right;
    RectA.Bottom:=SelectionRect.Bottom;
    ActiveSheet.SetMergedState(RectA,True);
  end;
end;

procedure TForm1.RzToolButton6Click(Sender: TObject);
begin
  CxExcel.ActiveSheet.ClearAll;
end;

procedure TForm1.RzToolButton7Click(Sender: TObject);
var
  StylA:TCxCellStyl;
begin
  CxExclLoad.FileName:='HelloWorld.xls';
  CxExclLoad.ShetLabl:='HelloWorld';
  CxExclLoad.ActvGrid:=AdvGrid;

  StylA:=TCxCellStyl.Create;
  StylA.IsfsBold:=True;
  StylA.DataType:=cxdtLongText;
  CxExclLoad.StrsStyl.AddObject(IntToStr(1),StylA);

  CxExclLoad.Execute;
end;

procedure TForm1.SetCommParams;
var
  I:Integer;
  NumbA:Integer;
begin
  RzSplitter1.Align:=alClient;
  RzSplitter1.HotSpotVisible:=True;
  RzSplitter1.Percent:=50;

  NumbA:=RzStatusBar1.Width div RzStatusBar1.ControlCount;

  for I := 0 to RzStatusBar1.ControlCount - 1 do
  begin
    TRzStatusPane(RzStatusBar1.Controls[I]).Width:=NumbA;
  end;

  Font.Style:=[fsBold];
end;

procedure TForm1.SetExclParams;
begin
  with CxExcel do
  begin
    DefaultRowHeight:=22;
  end;
end;

procedure TForm1.SetGridParams;
var
  I,M:Integer;
begin
  with AdvGrid do
  begin
    BeginUpdate;

    ClearRows(0,RowCount-1);

    RowCount:=40;
    ColCount:=12;

    for I := 0 to ColCount - 1 do
    begin
      for M := 0 to RowCount - 1 do
      begin
        //Cells[I,M]:=Format('Text:%D|%D',[I ,M]);
        cells[I,M]:='330226198501017036';
      end;
    end;

    EndUpdate;
  end;

end;

end.
